//
//  MyRankingVC.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/16.
//

import UIKit

class MyRankingVC: ZMViewController {
    
    @IBOutlet weak var mainScroll: UIScrollView!
    // 推广额度
    @IBOutlet weak var spreadNumberView: UIView!
    @IBOutlet weak var spreadNumber: UILabel!
    @IBOutlet weak var surSpreadNumberView: UIView!
    @IBOutlet weak var surSpreadNumber: UILabel!
    @IBOutlet weak var spreadDetailBtn: UIButton!
    
    // 累计出局额度
    @IBOutlet weak var outNumberView: UIView!
    @IBOutlet weak var outNumber: UILabel!
    // 剩余可用额度
    @IBOutlet weak var surplusOutNumberView: UIView!
    @IBOutlet weak var surplusOutNumber: UILabel!
    // 查看明细
    @IBOutlet weak var detailBtn: UIButton!
    
    @IBOutlet weak var rankView: UIView!
    @IBOutlet weak var rank: UILabel!
    
    @IBOutlet weak var listTable: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    
    let arr = [["id":"invite","img":"task_star","name":"邀请好友","tip":"邀请好友即可获得出局额度"],["id":"home","img":"task_money","name":"完成消费","tip":"完成消费获得出局额度"],["id":"sign","img":"task_sign","name":"知识付费","tip":"购买知识付费课程获得出局额度"]]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        setTable()
        bindAction()
        ZMToast.showActivity()
    }
    
    func setUI() {
        glt_scrollView = mainScroll
        view.backgroundColor = "#F5F5F5".zm.hex()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        spreadDetailBtn.style.GradientLeftToRight(Top: "#FFE4BF".zm.hex(), Bottom: "#FFFFFF".zm.hex())
        detailBtn.style.GradientLeftToRight(Top: "#FFE4BF".zm.hex(), Bottom: "#FFFFFF".zm.hex())
        rankView.style.GradientLeftToRight(Top: UIColor.zm.color(hexString: "#F3BAB2", 0.2), Bottom: "#FFFFFF".zm.hex())
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        requestInfo()
    }
    
    func setTable() {
        listTable.register(UINib(nibName: "MyRankingCell", bundle: nil), forCellReuseIdentifier: "MyRankingCell")
        listTable.rowHeight = UITableView.automaticDimension
        listTable.rowHeight = 60
        listTable.delegate = self
        listTable.dataSource = self
        listTable.separatorStyle = .none
        listTable.tableFooterView = UIView()
        let head = ZMLabel(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 54))
        head.zm_edgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0)
        head.text = "增加额度"
        head.font = UIFont(name: "PingFangSC-Medium", size: 16)
        head.textColor = "#333333".zm.hex()
        listTable.tableHeaderView = head
        listTable.reloadData()
    }
    
    // MARK: - bindAction
    func bindAction() {
//        let tap1 = UITapGestureRecognizer()
//        spreadNumberView.addGestureRecognizer(tap1)
//        tap1.rx.event.subscribe(onNext: { [unowned self] recognizer in
//            let vc = QuotaVC()
//            self.navigationController?.pushViewController(vc, animated: true)
//        }).disposed(by: disposeBag)
//
//        let tap2 = UITapGestureRecognizer()
//        surSpreadNumberView.addGestureRecognizer(tap2)
//        tap2.rx.event.subscribe(onNext: { [unowned self] recognizer in
//            let vc = QuotaVC()
//            vc.index = 1
//            self.navigationController?.pushViewController(vc, animated: true)
//        }).disposed(by: disposeBag)
//
//        let tap3 = UITapGestureRecognizer()
//        outNumberView.addGestureRecognizer(tap3)
//        tap3.rx.event.subscribe(onNext: { [unowned self] recognizer in
//            self.navigationController?.pushViewController(MyRankingDetailVC(), animated: true)
//        }).disposed(by: disposeBag)
//
//        let tap4 = UITapGestureRecognizer()
//        surplusOutNumberView.addGestureRecognizer(tap4)
//        tap4.rx.event.subscribe(onNext: { [unowned self] recognizer in
////            let vc = QuotaVC()
////            vc.index = 1
////            self.navigationController?.pushViewController(vc, animated: true)
//        }).disposed(by: disposeBag)
        
        listTable.rx.observeWeakly(CGSize.self, "contentSize").subscribe(onNext: { [unowned self] (change) in
            self.tableHeight.constant = change!.height
        }).disposed(by: disposeBag)
        
        spreadDetailBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.navigationController?.pushViewController(QuotaVC(), animated: true)
        }).disposed(by: disposeBag)
        
        detailBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.navigationController?.pushViewController(MyRankingDetailVC(), animated: true)
        }).disposed(by: disposeBag)
    }

}


extension MyRankingVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return UITableView.automaticDimension
//    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyRankingCell", for: indexPath) as! MyRankingCell
        let info = arr[indexPath.row]
        cell.img.image = UIImage(named: info["img"]!)
        cell.name.text = info["name"]!
        cell.tip.text = info["tip"]!
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let id = arr[indexPath.row]["id"]!
        if id == "invite" {
            router.push("xm://main/Mine/invite")
        } else if id == "sign" {
//            router.push("xm://main/Home/sign")
            tabBarController?.selectedIndex = 3
            navigationController?.popToRootViewController(animated: true)
        } else if id == "home" {
            tabBarController?.selectedIndex = 0
            navigationController?.popToRootViewController(animated: true)
        }
    }
}

// MARK: - Request Action
extension MyRankingVC {
    func requestInfo() {
        MineAPI.publicPlatoonInfo.rxRequestNullAble(type: [String: String].self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            if let data = resp {
                self.spreadNumber.text = data["spreadQuotaGive"] ?? "0"
                self.surSpreadNumber.text = data["spreadQuotaSurplus"] ?? "0"
                self.outNumber.text = data["quota"] ?? "0"
                self.surplusOutNumber.text = data["availableQuota"] ?? "0"
                self.rank.text = data["position"] ?? "0"
            }
        }).disposed(by: disposeBag)
    }
}
